package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Dto.ChannelConfigDto;
import com.dkd.manage.domain.Vo.ChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Service业务层处理
 * 
 * @author dcg
 * @date 2025-07-15
 */
@Service
public class ChannelServiceImpl implements IChannelService 
{
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel)
    {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }

    /**
     * 批量新增售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    public int batchInsertChannel(List<Channel> channel){
        return channelMapper.batchInsertChannel(channel);
    }

    /**
     * 批量查询售货机货道的商品
     *
     * @param skuIds 售货机货道
     * @return 售货机货道
     */
    @Override
    public int selectBySkuIds(Long[] skuIds) {
        return channelMapper.selectBySkuIds(skuIds);
    }
    /**
     * 根据售货机编号查询货道列表
     * @param innerCode 货道编码
     * @return ChannelVo集合
     */
    @Override
    public  List<ChannelVo> selectChannelVoByInnerCode(String innerCode)
    {
        return channelMapper.selectChannelVoByInnerCode(innerCode);
    }

    @Override
    public int setChannel(ChannelConfigDto channelConfigDto) {
        // 将dto对象转为po对象
        List<Channel> channelList = channelConfigDto.getChannelList().stream().map(channelSkuDto -> {
            // 查询对应的货道，这channel就是持久化对象
            Channel channel = channelMapper.selectChannelVoByInnerCodeAndChannelCode(channelSkuDto.getInnerCode(), channelSkuDto.getChannelCode());
            //更新货道的商品
            if(channel != null){
                // 更新商品
                channel.setSkuId(channelSkuDto.getSkuId());
                // 货道修改的时间
                channel.setUpdateTime(DateUtils.getNowDate());
            }
            // 每次返回一个channel货道对象
            return channel;
        }).collect(Collectors.toList());
        // 批量插入
        return channelMapper.batchUpdateChannel(channelList);
    }
}
